﻿/****************************** 模块头 ******************************\
* 模块名:  MailList.cs
* 项目名:  DingTalkAPIHelp
* 版权(c)  auth markeluo.
* 
*说明： MailList 用于调用钉钉API 通讯录相关接口
* This source is subject to the markeluo
* All other rights reserved.
\***************************************************************************/

using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using DingTalkAPIHelp.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DingTalkAPIHelp.MailList
{
    /// <summary>
    /// 通讯录管理
    /// </summary>
    public class MailListManager:SysConfigManager
    {
        private static MailListManager manager = new MailListManager();
        /// <summary>
        /// 通讯录管理实例(单例)
        /// </summary>
        public static MailListManager Instance
        {
            get
            {
                return manager;
            }
        }

        /// <summary>
        /// 1.获取通讯录权限范围(目前是开放全部权限)
        /// </summary>
        public OapiAuthScopesResponse AuthScopes()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/auth/scopes");
            OapiAuthScopesRequest request = new OapiAuthScopesRequest();
            request.SetHttpMethod("GET");
            OapiAuthScopesResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 2.获取部门列表(从根部门开始，获取所有的部门)
        /// </summary>
        public OapiDepartmentListResponse DeptList()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
            OapiDepartmentListRequest request = new OapiDepartmentListRequest();
            request.FetchChild = true;
            request.SetHttpMethod("GET");
            OapiDepartmentListResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 3.根据Id获取部门详情
        /// </summary>
        /// <param name="strDepId">部门id</param>
        /// <returns></returns>
        public OapiDepartmentGetResponse DepDetailById(string strDepId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/get");
            OapiDepartmentGetRequest request = new OapiDepartmentGetRequest();
            request.Id = strDepId;
            request.SetHttpMethod("GET");
            OapiDepartmentGetResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 4.获取部门下的用户列表(userid,Name)
        /// </summary>
        /// <param name="departmentId">部门Id</param>
        /// <param name="offset">启始位置</param>
        /// <param name="size">每页记录数</param>
        public OapiUserSimplelistResponse UserListByDept(long departmentId, long offset, long size)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
            OapiUserSimplelistRequest request = new OapiUserSimplelistRequest();
            request.Order = "entry_asc";//排序字段，默认按照进入部门的时间升序
            request.DepartmentId = departmentId;
            if (size > 0)
            {
                request.Offset = offset;
                request.Size = size;
            }
            request.SetHttpMethod("GET");

            OapiUserSimplelistResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 5.获取部门下的用户详情列表(userid,Name,JobNumber,...)
        /// </summary>
        /// <param name="departmentId">部门Id</param>
        /// <param name="offset">启始位置</param>
        /// <param name="size">每页记录数</param>
        public OapiUserListResponse UserDtailListByDept(long departmentId, long offset, long size)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/list");
            OapiUserListRequest request = new OapiUserListRequest();
            request.Order = "entry_asc";//排序字段，按照进入部门的时间升序
            request.DepartmentId = departmentId;
            if (size > 0)
            {
                request.Offset = offset;
                request.Size = size;
            }
            request.SetHttpMethod("GET");

            OapiUserListResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 6.获取员工信息-根据userId
        /// <param name="UserId">员工id</param>
        /// </summary>
        public OapiUserGetResponse UserInfo(string UserId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
            OapiUserGetRequest request = new OapiUserGetRequest();
            request.Userid = UserId;
            request.SetHttpMethod("GET");
            OapiUserGetResponse response = client.Execute(request, accessToken);
            return response;
        }
    }
}
